package io.renren.modules.job.task.crawl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.renren.modules.haiyou.dao.NewsDao;
import io.renren.modules.haiyou.entity.News;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * @auther: Freeman Liu
 * @date: 2020/11/4 15:00
 */
@Component
public class NewsPipeline implements Pipeline {

    @Resource
    private NewsDao newsDao;  // 这是生成的代理对象,如果用main方式执行,将为null

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> map = resultItems.getAll();

        News news = new News();
        try {
            BeanUtils.populate(news, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        if (newsDao.selectOne(new QueryWrapper<News>().lambda().eq(News::getTitle, news.getTitle())) == null) {
            news.setId(IdWorker.getIdStr());
            news.setHot(60);
            news.setAddTime(new Date());
            newsDao.insert(news);
        }
    }
}
